# -*- coding: utf-8 -*- 
# @Time : 2021/3/26/026 10:30 
# @Author : BuildRT
# @File : answer_search.py 

from py2neo import Graph

class AnswerSearcher:
    def __init__(self):
        self.g = Graph("http://localhost:7474", username="neo4j", password="1641442235wf")
        self.num_limit = 20

    '''执行cypher查询，并返回相应结果'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''根据对应的qustion_type，调用相应的回复模板'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''
        if question_type == 'groups_foodCategory':
            desc = [i['m.food_category_name'] for i in answers]
            subject = answers[0]['n.group_name']
            final_answer = '{0}中的食物分类包括：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'foodCategory_groups':
            desc = [i['m.food_category_name'] for i in answers]
            subject = answers[0]['n.group_name']
            final_answer = '{1}种类的食物是属于{0}分类的'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'foodCategory_food':
            desc = [i['m.food_name'] for i in answers]
            subject = answers[0]['n.food_category_name']
            final_answer = '{0}种类的食物有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'food_foodCategory':
            desc = [i['m.food_name'] for i in answers]
            subject = answers[0]['n.food_category_name']
            final_answer = '食物{1}是属于{0}种类的'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'food_energy':
            desc = []
            for i in answers:
                desc.append(i['m.nutrient_name']+':'+i['m.content'])
            subject = answers[0]['n.food_name']
            final_answer = '食物{0}的能量与相关成分有: {1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]) )

        elif question_type == 'food_vitamin':
            desc = []
            for i in answers:
                desc.append(i['m.nutrient_name'] + ':' + i['m.content'])
            subject = answers[0]['n.food_name']
            final_answer = '食物{0}的维生素含量有: {1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'food_mineral':
            desc = []
            for i in answers:
                desc.append(i['m.nutrient_name'] + ':' + i['m.content'])
            subject = answers[0]['n.food_name']
            final_answer = '食物{0}的矿物质含量有: {1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'food_fatty_acid':
            desc = []
            for i in answers:
                desc.append(i['m.nutrient_name'] + ':' + i['m.content'])
            subject = answers[0]['n.food_name']
            final_answer = '食物{0}的脂肪酸含量有: {1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        return final_answer


if __name__ == '__main__':
    searcher = AnswerSearcher()